home *** CD-ROM | disk | FTP | other *** search
- /*
- File: dprintf.c
-
- Contains: QuickDraw GX to PostScript conversion code.
-
- Version: Technology: Quickdraw GX 1.1.x
-
- Copyright: © 1994-1997 by Apple Computer, Inc., all rights reserved.
- */
-
- #include <stdio.h>
- #include <stdarg.h>
- #include <stdlib.h>
- #include <string.h>
-
- #ifndef __TYPES__
- #include <Types.h>
- #endif
-
- enum {
- kMaxDebugStringLength = 255
- };
-
-
- /*
- File: dprintf.c
-
- Contains: QuickDraw GX to PostScript conversion code.
-
- Version: Technology: Quickdraw GX 1.1.x
-
- Copyright: © 1994-1997 by Apple Computer, Inc., all rights reserved.
-
- You may incorporate this code into your applications without
- restriction, though the code has been provided "AS IS" and the
- responsibility for its operation is 100% yours. However, what you are
- not permitted to do is to redistribute the source as "Apple Sample
- Code" after having made changes. If you're going to re-distribute the
- source, we require that you make it clear in the source that the code
- was descended from Apple Sample Code, but that you've made changes.
- */
-
- /* ----------------------------------------------------------------------------------------
- Routine: dprintf
-
- Purpose: dprintf is equivelant to the Standard C library routine printf,
- except that the resulting text is sent to the debugger in a DebugStr
- call. This is especially useful in conjunction with Exceptions.h,
- which requires an externally declared dprintf for require and check
- to work at debugging levels other than DEBUGMIN.
- ---------------------------------------------------------------------------------------- */
-
- #include <GXExceptions.h>
- void dprintf(StringPtr debuggerCommand, char formatString[], ...)
- {
- va_list argumentList;
- int stringLength = 0;
- char debugString[kMaxDebugStringLength + 2],
- parameterString[kMaxDebugStringLength + 1],
- parameterFormatString[kMaxDebugStringLength],
- *currentParameterFormatString;
- char *currentChar, currentCharValue;
- int paramStringLength, combinedNewLength;
- int paramTruncated = 0;
- int argsWritten = 0;
- int stillFormatString, badParameter;
-
- int iValue;
- char *sValue;
- double dValue;
- void *pValue;
- int *iArgCount;
-
- #pragma unused(debuggerCommand)
-
- va_start(argumentList, formatString);
-
- for (currentChar = formatString; *currentChar && (stringLength < kMaxDebugStringLength); currentChar++) {
-
- currentCharValue = *currentChar;
-
- /* If the current format character is not a meta-character indicator, then just add the
- character to the debug string as-is and go on to the next character in the format
- string. */
-
- if (currentCharValue != '%') {
- debugString[++stringLength] = *currentChar;
- continue;
- }
-
- /* Otherwise we have a meta character %. Depending on the characters following the %,
- we have to create a parameter format string to pass to sprintf. The exact
- semantics of this are defined in K & R. */
-
- /* First add the meta character itself. */
-
- currentParameterFormatString = parameterFormatString;
- *currentParameterFormatString++ = '%';
-
- stillFormatString = true;
- badParameter = false;
-
- while (stillFormatString) {
-
- currentCharValue = *++currentChar;
-
- switch (currentCharValue) {
-
- /* Valid embedded characters for the meta-string. */
-
- case 'h': case 'H': case 'l': case 'L':
- case '+': case '-': case ' ': case '#': case '.':
- case '0': case '1': case '2': case '3': case '4':
- case '5': case '6': case '7': case '8': case '9':
- *currentParameterFormatString++ = currentCharValue;
- break;
-
- /* Valid termination string for the meta string. */
-
- case 'd': case 'i': case 'o': case 'X': case 'x':
- case 'u': case 'c': case 's': case 'f': case 'e':
- case 'E': case 'g': case 'G': case 'p': case 'n':
- case '%':
- *currentParameterFormatString++ = currentCharValue;
- stillFormatString = 0;
- break;
-
- /* Otherwise the character is not a valid termination
- of the meta string, so we should move the character
- back a character so it gets read again, and
- exit the parameter printing section of the code. */
-
- default:
- badParameter = true;
- stillFormatString = false;
- currentChar--;
- break;
-
- }
- }
-
- /* If the meta-character sequence was invalid, don’t bother calling through to
- sprintf for the actual parameter value. */
-
- if (badParameter) continue;
-
- /* Terminate the parater format string. */
-
- *currentParameterFormatString++ = '\0';
-
- /* Now depending on the last character in the meta-sequence, we need to
- pass a different type of argument through to sprintf to generate the
- parameter string. */
-
- switch (currentCharValue) {
-
- case 'd': case 'i': case 'o': case 'x': case 'X':
- case 'u': case 'c':
- iValue = va_arg(argumentList, int);
- sprintf(parameterString, parameterFormatString, iValue);
- break;
-
- case 's':
- sValue = va_arg(argumentList, char *);
- sprintf(parameterString, parameterFormatString, sValue);
- break;
-
- case 'f': case 'e': case 'E': case 'g': case 'G':
- dValue = va_arg(argumentList, double);
- sprintf(parameterString, parameterFormatString, dValue);
- break;
-
- case 'p':
- pValue = va_arg(argumentList, void *);
- sprintf(parameterString, parameterFormatString, pValue);
- break;
-
- case 'n':
- iArgCount = va_arg(argumentList, int *);
- *iArgCount = argsWritten;
- continue;
- break;
-
- case '%':
- parameterString[0] = '%';
- parameterString[1] = '\0';
- break;
-
- default:
- parameterString[0] = '\0';
- break;
-
- }
-
- /* If the parameter string will overflow the debug string’s kMaxDebugStringLength characters, then
- truncate it. */
-
- paramStringLength = strlen(parameterString);
- combinedNewLength = paramStringLength + stringLength;
-
- if (combinedNewLength > kMaxDebugStringLength) {
- paramStringLength = paramStringLength - (combinedNewLength - kMaxDebugStringLength);
- parameterString[paramStringLength] = '\0';
- paramTruncated = 1;
- }
-
- /* Add the parameter string to the debug string. */
-
- strcpy(debugString + stringLength + 1, parameterString);
-
- /* Advance the various info as necessary. */
-
- stringLength += paramStringLength;
- argsWritten++;
- }
-
- /* Make the pascal string the right length. */
-
- debugString[0] = stringLength;
-
- /* If we did not reach the end of the format string, or we had to truncate a
- parameter list, add a … to the string. */
-
- if ((*currentChar != 0) || paramTruncated) debugString[kMaxDebugStringLength] = '…';
-
- /* Output the pascal string to the debugger. */
-
- DebugStr((ConstStr255Param)debugString);
-
- va_end(argumentList);
-
- } // dprintf
-